////////////////////////////////////////////////////////////////////////////////
// Filename: cameraclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "cameraclass.h"

#include <D3DX10Math.h>

#define CAMERASPEED 0.25f


CameraClass::CameraClass()
{
	m_positionX = 0.0f;
	m_positionY = 0.0f;
	m_positionZ = 0.0f;

	m_rotationX = 0.0f;
	m_rotationY = 0.0f;
	m_rotationZ = 0.0f;
}


CameraClass::CameraClass(const CameraClass& other)
{
}


CameraClass::~CameraClass()
{
}


void CameraClass::SetPosition(float x, float y, float z)
{
	m_positionX = x;
	m_positionY = y;
	m_positionZ = z;
	return;
}


void CameraClass::SetRotation(float x, float y, float z)
{
	m_rotationX = x;
	m_rotationY = y;
	m_rotationZ = z;
	return;
}


void CameraClass::Render()
{
	D3DXVECTOR3 up;
	D3DXVECTOR3 position;

	D3DXVECTOR3 right;
	D3DXVECTOR3 camup;
	
	D3DXVECTOR3 lookAt;
	float radians;


	// Setup the vector that points upwards.
	up.x = 0.0f;
	up.y = 1.0f;
	up.z = 0.0f;

	// Setup the position of the camera in the world.
	position.x = m_positionX;
	position.y = m_positionY;
	position.z = m_positionZ;

	// Calculate the rotation in radians.
	radians = m_rotationY * 0.0174532925f;

	// Setup where the camera is looking.
	lookAt.x = m_positionX;
	lookAt.y = m_positionY;
	lookAt.z = m_positionZ;

	D3DXVec3Normalize(&lookAt, &lookAt);

	D3DXVec3Cross(&right, &lookAt, &up);
	D3DXVec3Cross(&camup, &right, &lookAt);

	D3DXVec3Normalize(&right, &right);
	D3DXVec3Normalize(&camup, &camup);

	if(CInput::GetInstance()->GetKey(DIK_W))
	{
		m_positionX -= lookAt.x * CAMERASPEED;
		m_positionY -= lookAt.y * CAMERASPEED;
		m_positionZ -= lookAt.z * CAMERASPEED;
	}

	if(CInput::GetInstance()->GetKey(DIK_S))
	{
		m_positionX += lookAt.x * CAMERASPEED;
		m_positionY += lookAt.y * CAMERASPEED;
		m_positionZ += lookAt.z * CAMERASPEED;
	}

	if(CInput::GetInstance()->GetKey(DIK_D))
	{
		m_positionX += right.x * CAMERASPEED;
		m_positionY += right.y * CAMERASPEED;
		m_positionZ += right.z * CAMERASPEED;
	}

	if(CInput::GetInstance()->GetKey(DIK_A))
	{
		m_positionX -= right.x * CAMERASPEED;
		m_positionY -= right.y * CAMERASPEED;
		m_positionZ -= right.z * CAMERASPEED;
	}

	if(CInput::GetInstance()->GetKey(DIK_Q))
	{
		m_positionX += camup.x * CAMERASPEED;
		m_positionY += camup.y * CAMERASPEED;
		m_positionZ += camup.z * CAMERASPEED;
	}

	if(CInput::GetInstance()->GetKey(DIK_E))
	{
		m_positionX -= camup.x * CAMERASPEED;
		m_positionY -= camup.y * CAMERASPEED;
		m_positionZ -= camup.z * CAMERASPEED;
	}

	// Create the view matrix from the three vectors.
	D3DXMatrixLookAtLH(&m_viewMatrix, &position, &lookAt, &up);

	return;
}


void CameraClass::GetViewMatrix(D3DXMATRIX& viewMatrix)
{
	viewMatrix = m_viewMatrix;
	return;
}